﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	public class TimeLine extends MovieClip {
		
		private var containersPanel:TL_ScenesDisplayer;
		private var mouseCoords:Vector.<Number>;
		//private var tagPicker_mc:TL_TagPicker;
		
		/**
		  Constructor
		*/
		public function TimeLine(){
			mouseCoords = new Vector.<Number>(2);
			
			containersPanel = new TL_ScenesDisplayer();
			containersPanel.y = 25;
			containersPanel.x = 0;
			
			// Adding children
			this.addChild(containersPanel);
			this.setChildIndex(overdeco_mc, numChildren-1);
			
			// Adding eventListeners
			this.addEventListener(MouseEvent.MOUSE_MOVE, refreshMouseCoords);
			this.addEventListener(Event.ENTER_FRAME, refreshDisplay);
		}
		
		/**
		  Sets the parent function to call after a scene has been selected
		*/
		public function setParentFunction(func:Function):void{
			containersPanel.setParentFunction(func);
		}
		
		/**
		  Returns if the TimeLine has changed since the last time it was shown
		*/
		public function hasChanged():Boolean{
			var changed:Boolean = false;
			if(containersPanel.hasChangedCurrentContainer()) changed = true;
			else if(containersPanel.hasChangedNumContainers()) changed = true;
			else if(containersPanel.hasChangedContainersType()) changed = true;
			return changed;
		}
		
		public function getSelectedContainer():uint{
			return containersPanel.getSelectedContainer();
		}
		
		public function getNumContainers():uint{
			return containersPanel.getNumContainers();
		}
		
		public function getContainersTypes():Vector.<uint>{
			return containersPanel.getContainersTypes();
		}
		
		/**
		  Sets the containers to show in the timeline
		  @param cont Vector.<Container> with all the containers
		  @param current uint specifying the index of the current container being modified
		*/
		public function setContainers(conts:Vector.<Container>, current:uint):void{
			tagPicker_mc.setText(String(current+1));
			
			containersPanel.refillPanels(conts, current);
		}
		
		/**
		  Displays in the text field the current selected container
		*/
		public function displaySelectedContainer(cont:uint):void{
			tagPicker_mc.setText(String(cont+1));
		}
		
		
		/**
		  Cleans all the contents of the timeline and its childs
		*/
		public function cleanAll():void{
			tagPicker_mc.setText(String(containersPanel.getSelectedContainer()+1));
			containersPanel.clearChildren();
		}
		
		/************** Private functions ************/
		private function refreshDisplay(evt:Event):void{
			if(mouseCoords[1] > -1000){
				if(mouseCoords[0] < 0)
					containersPanel.moveList((250-Math.abs(mouseCoords[0]))*-0.3 );
				else{
					containersPanel.moveList((250-Math.abs(mouseCoords[0]))* 0.3 );
				}
			}
				
		}
		
		private function refreshMouseCoords(evt:MouseEvent):void{
			if(Math.abs(evt.stageX-350) > 250 && evt.stageY > y+ containersPanel.y &&
								  evt.stageY < (y+ containersPanel.y +containersPanel.height) ){
				mouseCoords[0] = evt.stageX-350;
				mouseCoords[1] = evt.stageY;
			}else{
				mouseCoords[1] = -1000;
			}
		}
	}
}